/*     */ package com.zimbra.cs.zookeeper;
/*     */ 
/*     */ import com.google.common.io.Closeables;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import java.io.UnsupportedEncodingException;
/*     */ import java.util.Collection;
/*     */ import java.util.HashSet;
/*     */ import java.util.Set;
/*     */ import java.util.TreeSet;
/*     */ import org.apache.curator.RetryPolicy;
/*     */ import org.apache.curator.framework.CuratorFramework;
/*     */ import org.apache.curator.framework.CuratorFrameworkFactory;
/*     */ import org.apache.curator.framework.api.CreateBuilder;
/*     */ import org.apache.curator.framework.api.ExistsBuilder;
/*     */ import org.apache.curator.framework.api.GetDataBuilder;
/*     */ import org.apache.curator.framework.api.ProtectACLCreateModePathAndBytesable;
/*     */ import org.apache.curator.framework.api.SetDataBuilder;
/*     */ import org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex;
/*     */ import org.apache.curator.retry.ExponentialBackoffRetry;
/*     */ import org.apache.curator.x.discovery.ServiceCache;
/*     */ import org.apache.curator.x.discovery.ServiceCacheBuilder;
/*     */ import org.apache.curator.x.discovery.ServiceDiscovery;
/*     */ import org.apache.curator.x.discovery.ServiceDiscoveryBuilder;
/*     */ import org.apache.curator.x.discovery.ServiceInstance;
/*     */ import org.apache.curator.x.discovery.ServiceInstanceBuilder;
/*     */ import org.apache.curator.x.discovery.details.JsonInstanceSerializer;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CuratorManager
/*     */ {
/*     */   private final CuratorFramework client;
/*  46 */   private static CuratorManager instance = null;
/*     */   private final ServiceDiscovery<Service> serviceDiscovery;
/*     */   private final ServiceCache<Service> serviceCache;
/*     */   private final ServiceInstance<Service> thisInstance;
/*  50 */   private static boolean initialized = false;
/*     */   private static final String LOCKS = "/locks/";
/*     */   private static final String DATA = "/data/";
/*     */   private static final String SERVICE = "/service";
/*     */   
/*     */   private CuratorManager(String zkAddress) throws Exception
/*     */   {
/*  57 */     RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
/*  58 */     this.client = CuratorFrameworkFactory.newClient(zkAddress, retryPolicy);
/*  59 */     Server server = Provisioning.getInstance().getLocalServer();
/*  60 */     this.thisInstance = ServiceInstance.builder().name("mailbox").payload(new Service(server.getId())).build();
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*  65 */     JsonInstanceSerializer<Service> serializer = new JsonInstanceSerializer(Service.class);
/*     */     
/*     */ 
/*  68 */     this.serviceDiscovery = ServiceDiscoveryBuilder.builder(Service.class).client(this.client).basePath("/service").serializer(serializer).thisInstance(this.thisInstance).build();
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*  73 */     ServiceCacheBuilder<Service> serviceCacheBuilder = this.serviceDiscovery.serviceCacheBuilder().name("mailbox");
/*     */     
/*     */ 
/*  76 */     this.serviceCache = serviceCacheBuilder.build();
/*     */   }
/*     */   
/*     */   public static CuratorManager getInstance() throws ServiceException {
/*  80 */     if (!initialized) {
/*  81 */       return createInstance();
/*     */     }
/*  83 */     return instance;
/*     */   }
/*     */   
/*     */   private static synchronized CuratorManager createInstance() throws ServiceException {
/*  87 */     if (!initialized) {
/*  88 */       String[] servers = Provisioning.getInstance().getLocalServer().getZookeeperClientServerList();
/*  89 */       if (servers.length > 0) {
/*  90 */         TreeSet<String> tset = new TreeSet();
/*  91 */         for (int i = 0; i < servers.length; i++) {
/*  92 */           tset.add(servers[i].toLowerCase());
/*     */         }
/*  94 */         StringBuilder serverList = new StringBuilder();
/*  95 */         for (String s : tset) {
/*  96 */           if (serverList.length() > 0) {
/*  97 */             serverList.append(",");
/*     */           }
/*  99 */           serverList.append(s);
/*     */         }
/*     */         try {
/* 102 */           instance = new CuratorManager(serverList.toString());
/*     */         } catch (Exception e) {
/* 104 */           throw ServiceException.FAILURE("Could not initialize curator", e);
/*     */         }
/*     */       }
/* 107 */       initialized = true;
/*     */     }
/* 109 */     return instance;
/*     */   }
/*     */   
/*     */   public InterProcessSemaphoreMutex createLock(String id) {
/* 113 */     return new InterProcessSemaphoreMutex(this.client, "/locks/" + id);
/*     */   }
/*     */   
/*     */   public void setData(String key, String value) throws Exception {
/*     */     byte[] data;
/*     */     try {
/* 119 */       data = value.getBytes("UTF-8");
/*     */     } catch (UnsupportedEncodingException e) {
/* 121 */       data = value.getBytes();
/*     */     }
/* 123 */     String path = "/data/" + key;
/* 124 */     if (this.client.checkExists().forPath(path) == null) {
/* 125 */       this.client.create().creatingParentsIfNeeded().forPath(path);
/*     */     }
/* 127 */     this.client.setData().forPath("/data/" + key, data);
/*     */   }
/*     */   
/*     */   public String getData(String key) {
/*     */     try {
/* 132 */       byte[] data = (byte[])this.client.getData().forPath("/data/" + key);
/* 133 */       return new String(data, "UTF-8");
/*     */     } catch (Exception e) {}
/* 135 */     return null;
/*     */   }
/*     */   
/*     */   public Set<String> getActiveServers() throws Exception
/*     */   {
/* 140 */     Set<String> activeServers = new HashSet();
/* 141 */     Collection<ServiceInstance<Service>> instances = this.serviceCache.getInstances();
/* 142 */     for (ServiceInstance<Service> instance : instances) {
/* 143 */       activeServers.add(((Service)instance.getPayload()).getService());
/*     */     }
/* 145 */     return activeServers;
/*     */   }
/*     */   
/*     */   public void unregisterService(String serverId) throws Exception {
/* 149 */     Collection<ServiceInstance<Service>> instances = this.serviceCache.getInstances();
/* 150 */     for (ServiceInstance<Service> instance : instances) {
/* 151 */       if (serverId.equals(((Service)instance.getPayload()).getService())) {
/* 152 */         this.serviceDiscovery.unregisterService(instance);
/* 153 */         break;
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public void registerLocalService() throws Exception {
/* 159 */     this.serviceDiscovery.registerService(this.thisInstance);
/*     */   }
/*     */   
/*     */   public synchronized void start() throws Exception {
/* 163 */     if (this.client != null) {
/* 164 */       this.client.start();
/* 165 */       this.serviceDiscovery.start();
/* 166 */       this.serviceCache.start();
/*     */     }
/*     */   }
/*     */   
/*     */   public synchronized void stop() {
/* 171 */     if (this.client != null) {
/* 172 */       Closeables.closeQuietly(this.serviceCache);
/* 173 */       Closeables.closeQuietly(this.serviceDiscovery);
/* 174 */       Closeables.closeQuietly(this.client);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/zookeeper/CuratorManager.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */